﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Meeting.Entity.CommonEntity;
using XF.Meeting.Entity.DTO;
using XF.Meeting.Entity.Query;
using XF.Meeting.Entity.View;
using XF.Meeting.IDal;
using XF.Sugar.IService;

namespace XF.Meeting.Service
{
    public class MeetingService : BaseService, IMeetingService
    {

        private IMeetingDal dal = null;

        public MeetingService(string Operator, string OperaIP)
        {
            this.Operator = Operator;
            this.OperaIP = OperaIP;
            if (dal == null)
            {
                lock (this.OperaIP)
                {
                    if (dal == null)
                    {
                        dal = DalFactory.DalFactory.GetMeetingDaL();
                    }
                }
            }
        }

        private UEntity.LogEntity.ELogRecordEntity LogEntity = new UEntity.LogEntity.ELogRecordEntity();

        /// <summary>
        /// 会议预约
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> MeetingOrder(MeetingOrder entity)
        {
            string strError = "";
            bool bResult = true;
            int a = 0;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.MeetingOrder";
            LogEntity.OperateObject = "MeetingOrder";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<MeetingOrder>(entity);
            try
            {
                (bResult,a) = await dal.MeetingOrder(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("MeetingOrder Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return bResult;
        }

        /// <summary>
        /// 获取会议室及相关会议信息（首页预约展示）
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MeetAndRoomInfo> GetMeetRoomAndInfo(RoomQuery query)
        {
            MeetAndRoomInfo list = new MeetAndRoomInfo();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetMeetRoomAndInfo";
            LogEntity.OperateObject = "GetMeetRoomAndInfo";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RoomQuery>(query);
            try
            {
                var res = await dal.GetCRoomAndCInfo(query, out strError);

                if (strError == "")
                {
                  var meetLis=  TableToList.ToList<MeetingInfoMessage>(res.Item2);
                    meetLis.ForEach(i =>
                    {
                        if (i.IsAppRove==1)
                        {
                            if (i.BeginTime > DateTime.Now)
                            {
                                i.State = 1;
                            }
                            else if (i.BeginTime < DateTime.Now && i.EndTime > DateTime.Now)
                            {
                                i.State = 2;
                            }
                            else
                            {
                                i.State = 3;
                            }
                        }
                        else {
                            i.State = 4;
                        }
                    });
                    list.RoomList = TableToList.ToList<MeetingRoomMessage>(res.Item1);
                    list.MeetList = TableToList.ToList<MeetingInfoMessage>(res.Item2);
                    list.RecordCount = res.Item3;
                    list.PageCount = res.Item4;                  
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetMeetRoomAndInfo Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 根据区域ID获取当前区域下所有会议室信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<MeetingRoomInfo>> GetCRoomByRID(RoomQuery query)
        {
            PageList<MeetingRoomInfo> list = new PageList<MeetingRoomInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetCRoomByRID";
            LogEntity.OperateObject = "GetCRoomByRID";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RoomQuery>(query);
            try
            {
                var res = await dal.GetCRoomByRID(query, out strError);

                if (strError == "")
                {
                    list.List = TableToList.ToList<MeetingRoomInfo>(res.Item1);
                    list.RecordCount = res.Item2;
                    list.PageCount = res.Item3;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetCRoomByRID Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 根据查询关键字获取所有会议室信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<MeetingRoomInfo>> GetCRoomByKey(RoomQuery query)
        {
            PageList<MeetingRoomInfo> list = new PageList<MeetingRoomInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetCRoomByKey";
            LogEntity.OperateObject = "GetCRoomByKey";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RoomQuery>(query);
            try
            {
                var res = await dal.GetCRoomByKey(query, out strError);

                if (strError == "")
                {
                    list.List = TableToList.ToList<MeetingRoomInfo>(res.Item1);
                    list.RecordCount = res.Item2;
                    list.PageCount = res.Item3;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetCRoomByKey Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        private void RecordLog()
        {
            LogEntity.Operator = Operator;
            LogEntity.OperateIP = OperaIP;
            ULogWork.CLogToDataBase.RecordOperateLog(LogEntity);
        }
    }
}